﻿''' <summary>
''' This class is an utility class to include all Windows DeskTop management
''' It's a class that we can load info. to the AppManager. Similar Class will be needed for a web project.
''' 
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class DeskTopManager

    Public Shared ReadOnly Property SecurityFileNameUserColumnSecurity() As String
        Get
            If AppManager.UserID = New Guid("735B55FF-039D-4EBA-B996-D99C92B021B9") Or AppManager.UserName.ToLower = "sql_systemboxadmin" Then
                SecurityFileNameUserColumnSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("UserColumnSecurity{0}", AppManager.UserName.ToLower)))
            Else
                SecurityFileNameUserColumnSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("UserColumnSecurity{0}{1}{2}", AppManager.UserName.ToLower, AppManager.DbServerName.ToLower, AppManager.DbName.ToLower)))
            End If
        End Get
    End Property

    Public Shared ReadOnly Property SecurityFileNameDataFieldSecurity() As String
        Get
            If AppManager.UserID = New Guid("735B55FF-039D-4EBA-B996-D99C92B021B9") Or AppManager.UserName.ToLower = "sql_systemboxadmin" Then
                SecurityFileNameDataFieldSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("DataFieldSecurity{0}", AppManager.UserName.ToLower)))
            Else
                SecurityFileNameDataFieldSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("DataFieldSecurity{0}{1}{2}", AppManager.UserName.ToLower, AppManager.DbServerName.ToLower, AppManager.DbName.ToLower)))
            End If
        End Get
    End Property

    Public Shared ReadOnly Property SecurityFileNameDataSubjectSecurity() As String
        Get
            If AppManager.UserID = New Guid("735B55FF-039D-4EBA-B996-D99C92B021B9") Or AppManager.UserName.ToLower = "sql_systemboxadmin" Then
                SecurityFileNameDataSubjectSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("DataSubjectSecurity{0}", AppManager.UserName.ToLower)))
            Else
                SecurityFileNameDataSubjectSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("DataSubjectSecurity{0}{1}{2}", AppManager.UserName.ToLower, AppManager.DbServerName.ToLower, AppManager.DbName.ToLower)))
            End If
        End Get
    End Property

    Public Shared ReadOnly Property SecurityFileNameGroupListSecurity() As String
        Get
            If AppManager.UserID = New Guid("735B55FF-039D-4EBA-B996-D99C92B021B9") Or AppManager.UserName.ToLower = "sql_systemboxadmin" Then
                SecurityFileNameGroupListSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("GroupListSecurity{0}", AppManager.UserName.ToLower)))
            Else
                SecurityFileNameGroupListSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("GroupListSecurity{0}{1}{2}", AppManager.UserName.ToLower, AppManager.DbServerName.ToLower, AppManager.DbName.ToLower)))
            End If
        End Get
    End Property

    Public Shared ReadOnly Property SecurityFileNameSiteSecurity() As String
        Get
            If AppManager.UserID = New Guid("735B55FF-039D-4EBA-B996-D99C92B021B9") Or AppManager.UserName.ToLower = "sql_systemboxadmin" Then
                SecurityFileNameSiteSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("SiteSecurity{0}", AppManager.UserName.ToLower)))
            Else
                SecurityFileNameSiteSecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("SiteSecurity{0}{1}{2}", AppManager.UserName.ToLower, AppManager.DbServerName.ToLower, AppManager.DbName.ToLower)))
            End If
        End Get
    End Property

    Public Shared ReadOnly Property SecurityFileNameSocietySecurity() As String
        Get
            '
            'Le GUID ("735B55FF-039D-4EBA-B996-D99C92B021B9") est le USER sql_SystemBoxAdmin
            'Quand c'est le user sql_SystemBoxAdmin le systeme ouvre un fichier de configuration spécial qui permet d'être unique pour tous les systemes.
            'C'est le "backdoor" pour pouvoir configurer le system si nous n'avons pas de user ou de sécirité définis
            ' Un fichier de sécurité standard est fait de Username + dbServerName + dbName 
            ' celui de sql_SystemBoxAdmin ne contient que le UserName, alors il va avoir toutes les memes sécurité sur tous les serveurs sans etre obligé de le regénéré a chaque fois...
            '
            If AppManager.UserID = New Guid("735B55FF-039D-4EBA-B996-D99C92B021B9") Or AppManager.UserName.ToLower = "sql_systemboxadmin" Then
                SecurityFileNameSocietySecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("SocietySecurity{0}", AppManager.UserName.ToLower)))
            Else
                SecurityFileNameSocietySecurity = IO.Path.Combine(DeskTopManager.ApplicationDataFolder, CommonExt.Utils.FileExt.EncodedFileName(String.Format("SocietySecurity{0}{1}{2}", AppManager.UserName.ToLower, AppManager.DbServerName.ToLower, AppManager.DbName.ToLower)))
            End If
        End Get
    End Property

    Public Shared Property ApplicationFolder() As String = Application.StartupPath
    Public Shared Property ApplicationDataFolder() As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\SCIEF"

    Public Shared Sub GetSecurityProfile()
        Dim siteGUIDS As String = Nothing
        Dim societyGUIDS As String = Nothing
        'testpoint: Access List are here
        AppManager.MetaColumnAccess = SystemBoxSDK.PL.MetaSDK.SecurityProfiler_PL.SecurityProfileDS.Tables("UserColumnSecurity")
        AppManager.DataFieldAccess = SystemBoxSDK.PL.MetaSDK.SecurityProfiler_PL.SecurityProfileDS.Tables("DataFieldSecurity") 'SecurityProfiler_PL.GenerateDataFieldSecurity(AppManager.UserID)
        AppManager.DataSubjectAccess = SystemBoxSDK.PL.MetaSDK.SecurityProfiler_PL.SecurityProfileDS.Tables("DataSubjectSecurity") 'SecurityProfiler_PL.GenerateDataSubjectSecurity(AppManager.UserID)
        AppManager.GroupListAccess = SystemBoxSDK.PL.MetaSDK.SecurityProfiler_PL.SecurityProfileDS.Tables("GroupListSecurity")

        '
        Using aSiteDT As DataTable = SystemBoxSDK.PL.MetaSDK.SecurityProfiler_PL.SecurityProfileDS.Tables("SiteSecurity")
            'SecurityProfiler_PL.GenerateSiteSecurity(AppManager.UserID)
            If aSiteDT.Rows.Count > 0 Then
                For Each arow As DataRow In aSiteDT.Rows
                    If Not IsDBNull(arow("AllowRead")) AndAlso CType(arow("AllowRead"), Boolean) = True Then
                        siteGUIDS += IIf(siteGUIDS Is Nothing, String.Format("'{0}'", arow("SiteId")), String.Format(",'{0}'", arow("SiteId"))).ToString
                    End If
                Next
                If siteGUIDS Is Nothing Then
                    siteGUIDS = String.Format("'{0}'", Guid.Empty)
                End If
            End If
        End Using
        AppManager.SiteAccessList = siteGUIDS
        '
        Using aSocietyDT As DataTable = SystemBoxSDK.PL.MetaSDK.SecurityProfiler_PL.SecurityProfileDS.Tables("SocietySecurity")
            'SecurityProfiler_PL.GenerateSocietySecurity(AppManager.UserID)
            If aSocietyDT.Rows.Count > 0 Then
                For Each arow As DataRow In aSocietyDT.Rows
                    If Not IsDBNull(arow("AllowRead")) AndAlso CType(arow("AllowRead"), Boolean) = True Then
                        societyGUIDS += IIf(societyGUIDS Is Nothing, String.Format("'{0}'", arow("SocietyId")), String.Format(",'{0}'", arow("SocietyId"))).ToString
                    End If
                Next
                If societyGUIDS Is Nothing Then
                    societyGUIDS = String.Format("'{0}'", Guid.Empty)
                End If
            End If
        End Using
        'Testpoint : Society Access List
        AppManager.SocietyAccessList = societyGUIDS
    End Sub

    ''' <summary>
    ''' Get the DefaultText from MetaString base on the Current Language
    ''' </summary>
    ''' <param name="code"></param>
    ''' <returns>Text from the Metastring, if not found String.Empty</returns>
    ''' <remarks>Get the DefaultText from MetaString base on the Current Language. 
    ''' Language is set in Option as: Language. do not confuse with PRM or ALT display column</remarks>
    Public Shared Function GetMessage(ByVal code As String) As String
        Try
            Dim ms As MetaString = MetaString.GetMetaStringByCode(code, AppManager.ApplicationOption.LanguageId)
            If ms Is Nothing Then
                Return String.Format("MetaString [{0}] not defined / Non définie", code)
            End If
            Return ms.DefaultText
        Catch ex As Exception
            Return String.Format("MetaString [{0}] Please connect to a Database / SVP connecter vous a une base de donnée", code)
        End Try
    End Function
End Class
